package com.memory.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.memory.common.R;
import com.memory.dao.CategoryDao;
import com.memory.dao.DishDao;
import com.memory.dao.SetMealDao;
import com.memory.daomain.Category;
import com.memory.daomain.Dish;
import com.memory.daomain.Setmeal;
import com.memory.exception.CustomException;
import com.memory.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 邓哈哈
 * 2023/1/16 13:31
 * Function:
 * Version 1.0
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {
    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private DishDao dishDao;
    @Autowired
    private SetMealDao setMealDao;

    /**
     * 分页查询
     * 条件查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Page getPage(Integer page, Integer pageSize) {
        //1.封装Page对象
        Page<Category> pageInfo = new Page(page, pageSize);

        //2.设置查询条件
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(Category::getSort);

        //3.执行分页查询
        categoryDao.selectPage(pageInfo, lqw);

        return pageInfo;
    }

    /**
     * 删除分类
     * 删除分类时,需要判断该分类下是否还有菜品或套餐属于这个分类,有,就不能删
     *
     * @param id
     * @return
     */
    @Override
    public Boolean remove(Long id) {
        //1.判断该分类下是否还有菜品属于这个分类
        //添加查询条件
        LambdaQueryWrapper<Dish> dish_lqw = new LambdaQueryWrapper<>();
        //返回该分类下菜品的数量
        dish_lqw.eq(id != null, Dish::getCategoryId, id);//匹配该分类相关联的菜品
        Integer dish_count = dishDao.selectCount(dish_lqw);//返回数量

        if (dish_count > 0) {//该分类存在菜品关联,删除失败,抛出一个业务异常
            throw new CustomException("该分类存在菜品关联,删除失败");
        }

        //2.判断该分类下是否还有菜品属于这个分类
        //添加查询条件
        LambdaQueryWrapper<Setmeal> meal_lqw = new LambdaQueryWrapper<>();
        meal_lqw.eq(id != null, Setmeal::getCategoryId, id);//匹配该分类相关联的套餐
        Integer meal_count = setMealDao.selectCount(meal_lqw);//返回数量

        if (meal_count > 0) {//该分类存在套餐关联,删除失败,抛出一个业务异常
            throw new CustomException("该分类存在套餐关联,删除失败");
        }

        categoryDao.deleteById(id);

        //可以正常删除
        return true;
    }

    /**
     * 获取菜品分类下的所有菜品
     *
     * @param category
     * @return
     */
    @Override
    public List<Category> getType(Category category) {
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        //如果没有传菜品分类还是套餐分类,默认全部查询
        if (category.getType() == null) {
            List<Category> categoryList = this.list();
            return categoryList;
        }
        //select * from category

        //设置查询条件
        lqw.eq(category != null, Category::getType, category.getType());
        //设置排序条件
        lqw.orderByDesc(Category::getUpdateTime);

        List<Category> categoryList = this.list(lqw);

        return categoryList;
    }
}

